/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socof.wget;


/**
 *
 * @author Carlos Pinheiro
 */
public class MainLaucher extends Thread
{
    private static final int WAIT_TIME = 1000;
    
    private static MainLaucher lb = new MainLaucher();
    private DownloadersLauncher downloadersLauncher;
    private DownloadersBalancer downloadersController;
    private ParsersLauncher parsersLauncher;
    private ParsersBalancers parsersController;
    
    private MainLaucher()
    {
        this.downloadersLauncher = DownloadersLauncher.getInstance();
        this.downloadersController = DownloadersBalancer.getInstance();
        this.parsersLauncher = ParsersLauncher.getInstance();
        this.parsersController = ParsersBalancers.getInstance();
    }
    
    public static MainLaucher getInstance()
    {
        return MainLaucher.lb;
    }
    
    public void setMaxBandwithAllocation (long maxBandwithAllocation)
    {
        this.downloadersController.setMaxBandwidthAllocation(maxBandwithAllocation);
    }
    
    public void setMaxCpuUsage (double maxCpuUsage)
    {
        this.parsersController.setMaxCpuUsage(maxCpuUsage);
    }
    
    public void setDownloadersLimit(int downloadersLimit)
    {
        this.downloadersController.setDownloadersLimit(downloadersLimit);
    }

    public void setParsersLimit(int parsersLimit)
    {
        this.parsersController.setParsersLimit(parsersLimit);
    }
    
    public synchronized void pauseProcess ()
    {
        this.downloadersController.setPaused(true);
        this.parsersController.setPaused(true);
    }
    
    public synchronized void resumeProcess()
    {
        this.downloadersController.setPaused(false);
        this.parsersController.setPaused(false);
    }
    
    @Override
    public void run()
    {
        try
        {
            this.downloadersLauncher.start();
            this.parsersLauncher.start();
            
            // Let the process start
            Thread.sleep(WAIT_TIME);
            
            int attempts = 0;
            while (!isInterrupted())
            {
                // If all Queues are empty, finish de process !!!
                if (ToDownloadQueue.getInstance().isEmpty() &&
                    ToParseQueue.getInstance().isEmpty() &&
                    this.downloadersController.getNumberOfDownloaders() > 0 &&
                    this.parsersController.getNumberOfParsers() > 0)
                {
                    if (attempts >= 3)
                        throw new InterruptedException("All queues are empty, finishing process.");
                    else
                        attempts++;
                }
                
                attempts = 0;
                
                Thread.sleep(WAIT_TIME);
            }
            
            this.downloadersLauncher.join();
            this.parsersLauncher.join();
            
        }
        catch (InterruptedException ex)
        {
            this.downloadersLauncher.interrupt();
            this.parsersLauncher.interrupt();
            
            try
            {
                this.downloadersLauncher.join();
                this.parsersLauncher.join();
            }
            catch (InterruptedException ex1)
            {
                //Logger.getLogger(MainLaucher.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }    
}
